Imports BlazeApps.Library.Form
Imports System.Data
Imports AjaxControlToolkit

Partial Class Usercontrols_Form_FormManager
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property FormGuid() As Guid
        Get
            Return GetGuidFromVS("FormID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("FormID", value)
        End Set
    End Property

    Protected Property FormQuestionGuid() As Guid
        Get
            Return GetGuidFromVS("FormQuestionID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("FormQuestionID", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Form Manager")

        If IsUserAuthenticated And (EnableUserBlogs OrElse IsEditor()) Then
            GetQueryStringValues()

            If Me.Page.IsPostBack = False Then
                LoadControlData()
            End If

            GetFormDetails()

			BuildPage(FormGuid, Guid.Empty, Me.uxControlsPlaceHolder, "formbuilder", False)

			Me.uxFormHyperLink.NavigateUrl = String.Format("{0}.aspx?pid=form_form&formid={1}", AppPage, FormGuid)
			Me.uxFormHyperLink.Text = "Link to the form that can be listed on the menu or sent to people directly."
        Else
            Me.uxFormManagerPanel.Visible = False
            Me.uxMessagesLabel.Text = ShowControls(SecurityMask.Editor, True)
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveForm()
        Dim item As FormDetail = FormDetail.NewFormDetail()

        item.FormName = Me.uxFormNameTextBox.Text
        item.AllowMultipleResponses = Me.uxAllowMultipleResponsesCheckBox.Checked
        item.IvFormtype = Convert.ToInt32(Me.uxFormTypeDropDownList.SelectedValue)
        item.ExpireDateString = Me.uxExpirationDateTextBox.Text
		item.UserID = CurrentUserID()
		item.RegisteredUserOnly = Me.uxRegisteredUsersOnlyCheckBox.Checked
        item.FormCompleteMessage = Me.uxFormCompleteMessageTextBox.Text

        item.Save()

		FormGuid = item.FormID

		Me.uxFormHyperLink.NavigateUrl = String.Format("{0}.aspx?pid=form_form&formid={1}", AppPage, FormGuid)

    End Sub

    Private Sub UpdateForm()
        Dim item As FormDetail = FormDetail.GetFormDetail(FormGuid)

        item.FormName = Me.uxFormNameTextBox.Text
        item.AllowMultipleResponses = Me.uxAllowMultipleResponsesCheckBox.Checked
        item.IvFormtype = Convert.ToInt32(Me.uxFormTypeDropDownList.SelectedValue)
        item.ExpireDateString = Me.uxExpirationDateTextBox.Text
		item.FormCompleteMessage = Me.uxFormCompleteMessageTextBox.Text
		item.RegisteredUserOnly = Me.uxRegisteredUsersOnlyCheckBox.Checked

        item.Save()
	End Sub

    Private Sub SaveFormQuestion()
        Dim Item As FormQuestionDetail = FormQuestionDetail.NewFormQuestionDetail()

        Item.FormID = FormGuid
        Item.Question = Me.uxQuestionTextBox.Text.Trim()
        Item.FormControlID = New Guid(Me.uxFormControlsDropDownList.SelectedValue.ToString.Trim)
        Item.FormPage = 1
        Item.CreateDateString = DateTime.Now().ToString()

        If uxRequiredPanel.Visible Then
            Item.IsRequired = Me.uxQuestionRequiredCheckBox.Checked
            Item.RequiredText = Me.uxRequiredMessageTextbox.Text.Trim()
        End If

		If uxOtherOptionPanel.Visible Then
			Item.IsOtherOption = Me.uxOtherOptionCheckBox.Checked
			Item.OtherOptionText = Me.uxOtherOptionTextBox.Text.Trim()
		End If

        If uxValidatorPanel.Visible Then
            If (Convert.ToInt32(Me.uxValidatorTypeDropDownList.SelectedValue) >= 1 And Convert.ToInt32(Me.uxValidatorTypeDropDownList.SelectedValue) <= 5) Then
                Item.IsCompareValidatorRequired = Me.uxValidatorCheckBox.Checked
                Item.CompareValidatorType = Convert.ToInt32(Me.uxValidatorTypeDropDownList.SelectedValue)
                Item.CompareValidatorText = Me.uxValidatorWarningMessaegTextBox.Text.Trim()
            Else
                Item.IsRegularExpressionValidatorRequired = Me.uxValidatorCheckBox.Checked
                Item.RegularExpression = GetRegularExpression(CType(Me.uxValidatorTypeDropDownList.SelectedValue, SecondaryValidatorTypes))
                Item.RegularExpressionValidatorText = Me.uxValidatorWarningMessaegTextBox.Text.Trim()
            End If
        End If

        If uxAnswerChoicesPanel.Visible Then

            If uxBuildChoicesPanel.Visible Then
                Dim AnswerChoices() As String
                Dim choice As Object

                AnswerChoices = Me.uxAnswerChoicesTextBox.Text.Split(Environment.NewLine)

                For Each choice In AnswerChoices

                    Dim QR As FormQuestionRowsDetail = FormQuestionRowsDetail.NewFormQuestionRowsDetail()

                    QR.FormQuestionID = Item.FormQuestionID
                    QR.RowChoice = choice
                    QR.CreateDateString = DateTime.Now().ToString()

                    QR.Save()

				Next

				If uxOtherOptionPanel.Visible And Me.uxOtherOptionCheckBox.Checked Then

					Dim QR As FormQuestionRowsDetail = FormQuestionRowsDetail.NewFormQuestionRowsDetail()

					QR.FormQuestionID = Item.FormQuestionID
					QR.RowChoice = Me.uxOtherOptionTextBox.Text.Trim()
					QR.CreateDateString = DateTime.Now().ToString()

					QR.Save()

				End If

            End If

            If uxBuildColumnPanel.Visible Then
                Dim AnswerColumns() As String
                Dim choice As Object

                AnswerColumns = Me.uxAnswerColumnsTextBox.Text.Split(Environment.NewLine)

                For Each choice In AnswerColumns

                    Dim QC As FormQuestionColumsDetail = FormQuestionColumsDetail.NewFormQuestionColumsDetail()

                    QC.FormQuestionID = Item.FormQuestionID
                    QC.ColumnChoice = choice
                    QC.CreateDateString = DateTime.Now().ToString()

                    QC.Save()

                Next
            End If

        End If

        If uxQuestionAttributesPanel.Visible And uxMultiTextBoxPanel.Visible Then
            Item.FormControlRows = Convert.ToInt32(Me.uxRowsDropDownList.SelectedValue)
            Item.FormControlCols = Convert.ToInt32(Me.uxColumnsDropDownList.SelectedValue)
        End If

        If uxQuestionAttributesPanel.Visible And uxChoiceLayoutPanel.Visible Then
            Item.FormControlLayout = Convert.ToInt32(Me.uxDirectionLayoutDropdownlist.SelectedValue)
        End If

        Item.Save()

        BuildPage(FormGuid, Guid.Empty, Me.uxControlsPlaceHolder, "formbuilder", False)
    End Sub

    Private Sub UpdateFormQuestion()
        Dim Item As FormQuestionDetail = FormQuestionDetail.GetFormQuestionDetail(FormQuestionGuid)

        Item.Question = Me.uxQuestionTextBox.Text.Trim()

        If uxRequiredPanel.Visible Then
            Item.IsRequired = Me.uxQuestionRequiredCheckBox.Checked
            Item.RequiredText = Me.uxRequiredMessageTextbox.Text.Trim()
		End If

		If uxOtherOptionPanel.Visible Then
			Item.IsOtherOption = Me.uxOtherOptionCheckBox.Checked
			Item.OtherOptionText = Me.uxOtherOptionTextBox.Text.Trim()
		End If

        If uxValidatorPanel.Visible Then
            If (Convert.ToInt32(Me.uxValidatorTypeDropDownList.SelectedValue) >= 1 And Convert.ToInt32(Me.uxValidatorTypeDropDownList.SelectedValue) <= 5) Then
                Item.IsCompareValidatorRequired = Me.uxValidatorCheckBox.Checked
                Item.CompareValidatorType = Convert.ToInt32(Me.uxValidatorTypeDropDownList.SelectedValue)
                Item.CompareValidatorText = Me.uxValidatorWarningMessaegTextBox.Text.Trim()
            Else
                Item.IsRegularExpressionValidatorRequired = Me.uxValidatorCheckBox.Checked
                Item.RegularExpression = GetRegularExpression(CType(Me.uxValidatorTypeDropDownList.SelectedValue, SecondaryValidatorTypes))
                Item.RegularExpressionValidatorText = Me.uxValidatorWarningMessaegTextBox.Text.Trim()
            End If
        End If

        If uxAnswerChoicesPanel.Visible Then

            If uxBuildChoicesPanel.Visible Then
                Dim AnswerChoices() As String
                Dim choice As Object

                AnswerChoices = Me.uxAnswerChoicesTextBox.Text.Split(Environment.NewLine)

                For Each choice In AnswerChoices

                    Dim QC As FormQuestionRowsDetail = FormQuestionRowsDetail.NewFormQuestionRowsDetail()

                    QC.FormQuestionID = Item.FormQuestionID
                    QC.RowChoice = choice

                    QC.Save()

				Next

				If uxOtherOptionPanel.Visible And Me.uxOtherOptionCheckBox.Checked Then

					Dim QR As FormQuestionRowsDetail = FormQuestionRowsDetail.NewFormQuestionRowsDetail()

					QR.FormQuestionID = Item.FormQuestionID
					QR.RowChoice = Me.uxOtherOptionTextBox.Text.Trim()
					QR.CreateDateString = DateTime.Now().ToString()

					QR.Save()

				End If

            End If

        End If

        If uxQuestionAttributesPanel.Visible And uxMultiTextBoxPanel.Visible Then
            Item.FormControlRows = Convert.ToInt32(Me.uxRowsDropDownList.SelectedValue)
            Item.FormControlCols = Convert.ToInt32(Me.uxColumnsDropDownList.SelectedValue)
        End If

        If uxQuestionAttributesPanel.Visible And uxChoiceLayoutPanel.Visible Then
            Item.FormControlLayout = Convert.ToInt32(Me.uxDirectionLayoutDropdownlist.SelectedValue)
        End If

        Item.Save()

        BuildPage(FormGuid, Guid.Empty, Me.uxControlsPlaceHolder, "formbuilder", False)
    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadControlData()
        LoadFormTypeList(Me.uxFormTypeDropDownList)
        LoadControlPickerList(Me.uxFormControlsDropDownList)
        LoadRows(Me.uxRowsDropDownList)
        LoadCols(Me.uxColumnsDropDownList)
        LoadComparValidatorTypes(Me.uxValidatorTypeDropDownList)
        LoadChoiceLayout(Me.uxDirectionLayoutDropdownlist)
    End Sub

    Private Sub GetFormDetails()
        If FormGuid <> Guid.Empty Then
            Dim item As FormDetail = FormDetail.GetFormDetail(FormGuid)

            Me.uxFormNameTextBox.Text = item.FormName
            Me.uxAllowMultipleResponsesCheckBox.Checked = item.AllowMultipleResponses
            Me.uxFormTypeDropDownList.SelectedValue = item.IvFormtype.ToString().Trim()
            Me.uxExpirationDateTextBox.Text = item.ExpireDate.ToShortDateString()
			Me.uxFormCompleteMessageTextBox.Text = item.FormCompleteMessage.ToString().Trim()
			Me.uxRegisteredUsersOnlyCheckBox.Checked = item.RegisteredUserOnly

        End If
    End Sub

    Private Sub LoadControlEditor(ByVal FormControlID As Guid, ByVal EditMode As Boolean)
        Dim item As FormControlsDetail = FormControlsDetail.GetFormControlsDetail(FormControlID)

        Select Case CType(item.ControlType, FormControlType)
            Case FormControlType.SingleTextBox

                SetRequiredPanelState(True)
                SetQuestionAttributesPanelState(False)
                SetMultiTextBoxPanelState(False)
                SetValidatorPanelState(True)
                SetChoiceLayoutPanelState(False)
                SetBuildChoicesPanelState(False)
                SetBuildColumnPanelState(False)
				SetAnswerChoicePanelState(False, False)
				SetuxOtherOptionPanelState(False)

            Case FormControlType.MultilineTextBox

                SetRequiredPanelState(True)
                SetQuestionAttributesPanelState(True)
                SetMultiTextBoxPanelState(True)
                SetValidatorPanelState(False)
                SetChoiceLayoutPanelState(False)
                SetBuildChoicesPanelState(False)
                SetBuildColumnPanelState(False)
				SetAnswerChoicePanelState(False, False)
				SetuxOtherOptionPanelState(False)

            Case FormControlType.SelectionList

                SetRequiredPanelState(True)
                SetQuestionAttributesPanelState(False)
                SetMultiTextBoxPanelState(False)
                SetValidatorPanelState(False)
                SetChoiceLayoutPanelState(False)
                SetBuildChoicesPanelState(True)
                SetBuildColumnPanelState(False)
				SetAnswerChoicePanelState(True, EditMode)
				SetAnswerColumnPanelState(False, EditMode)
				SetuxOtherOptionPanelState(False)

            Case FormControlType.DateTimeBox

                SetRequiredPanelState(True)
                SetQuestionAttributesPanelState(False)
                SetMultiTextBoxPanelState(False)
                SetValidatorPanelState(True)
                SetChoiceLayoutPanelState(False)
                SetBuildChoicesPanelState(False)
                SetBuildColumnPanelState(False)
				SetAnswerChoicePanelState(False, False)
				SetuxOtherOptionPanelState(False)

            Case FormControlType.MultipleChoiceSingleAnswer

				SetRequiredPanelState(True)
                SetQuestionAttributesPanelState(True)
                SetMultiTextBoxPanelState(False)
                SetValidatorPanelState(False)
                SetChoiceLayoutPanelState(True)
                SetBuildChoicesPanelState(True)
                SetBuildColumnPanelState(False)
				SetAnswerChoicePanelState(True, EditMode)
				SetAnswerColumnPanelState(False, EditMode)
				SetuxOtherOptionPanelState(True)

            Case FormControlType.MultipleChoiceMultipleAnswer

				SetRequiredPanelState(True)
                SetQuestionAttributesPanelState(True)
                SetMultiTextBoxPanelState(False)
				SetValidatorPanelState(False)
                SetChoiceLayoutPanelState(True)
                SetBuildChoicesPanelState(True)
                SetBuildColumnPanelState(False)
				SetAnswerChoicePanelState(True, EditMode)
				SetAnswerColumnPanelState(False, EditMode)
				SetuxOtherOptionPanelState(True)

            Case FormControlType.MatrixOneAnswerPerRow

                SetRequiredPanelState(False)
                SetQuestionAttributesPanelState(False)
                SetMultiTextBoxPanelState(False)
                SetValidatorPanelState(False)
                SetChoiceLayoutPanelState(False)
                SetBuildChoicesPanelState(True)
                SetBuildColumnPanelState(True)
				SetAnswerChoicePanelState(True, EditMode)
				SetAnswerColumnPanelState(True, EditMode)
				SetuxOtherOptionPanelState(False)

            Case FormControlType.MatrixMultipleAnswersPerRow

                SetRequiredPanelState(False)
                SetQuestionAttributesPanelState(False)
                SetMultiTextBoxPanelState(False)
                SetValidatorPanelState(False)
                SetChoiceLayoutPanelState(False)
                SetBuildChoicesPanelState(True)
                SetBuildColumnPanelState(True)
				SetAnswerChoicePanelState(True, EditMode)
				SetAnswerColumnPanelState(True, EditMode)
				SetuxOtherOptionPanelState(False)

        End Select
    End Sub

    Private Sub BindSelectionListItems(ByVal QuestionID As Guid)
        Dim list As New FormList()

		If uxEditChoicesPanel.Visible Then
			With Me.uxCurrentAnswerChoicesDropDownList
				.DataSource = list.GetQuestionRows(QuestionID)
				.DataTextField = "RowChoice"
				.DataValueField = "FormRowID"
				.DataBind()
			End With
		End If

		If uxEditColumnsPanel.Visible Then
			With Me.uxCurrentAnswerColumnsDropDownList
				.DataSource = list.GetQuestionColumns(QuestionID)
				.DataTextField = "ColumnChoice"
				.DataValueField = "FormColumnID"
				.DataBind()
			End With
		End If

    End Sub

    Private Sub LoadEditDetails(ByVal QuestionID As Guid)
        Dim QuestionDetail As FormQuestionDetail = FormQuestionDetail.GetFormQuestionDetail(QuestionID)
        Dim ControlDetail As FormControlsDetail = FormControlsDetail.GetFormControlsDetail(QuestionDetail.FormControlID)

        LoadControlEditor(QuestionDetail.FormControlID, True)

        Me.uxQuestionTextBox.Text = QuestionDetail.Question
        Me.uxFormControlsDropDownList.SelectedValue = QuestionDetail.FormControlID.ToString().Trim()
        Me.uxFormControlsDropDownList.Enabled = False

        Select Case CType(ControlDetail.ControlType, FormControlType)
			Case FormControlType.MultipleChoiceMultipleAnswer, FormControlType.MultipleChoiceSingleAnswer, FormControlType.MatrixOneAnswerPerRow, FormControlType.MatrixMultipleAnswersPerRow

				BindSelectionListItems(QuestionID)

		End Select

        Me.uxControlPickerModalPopupExtender.Show()
    End Sub

    Private Sub ClearEditDetails()

        Me.uxFormControlsDropDownList.Enabled = True
        Me.uxQuestionTextBox.Text = String.Empty
        Me.uxCurrentAnswerChoicesDropDownList.Items.Clear()
        Me.uxAddAnswerChoiceTextBox.Text = String.Empty

        Me.uxControlPickerModalPopupExtender.Show()

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        If FormGuid = Guid.Empty Then
            SaveForm()
        Else
            UpdateForm()
        End If
    End Sub

    Protected Sub uxSaveControlButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveControlButton.Click

        If FormQuestionGuid = Guid.Empty Then
            SaveFormQuestion()
        Else
            UpdateFormQuestion()
        End If

    End Sub

    Protected Sub uxFormControlsDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxFormControlsDropDownList.SelectedIndexChanged

        LoadControlEditor(ConvertToGuid(Me.uxFormControlsDropDownList), False)

    End Sub

    Protected Sub uxCurrentAnswerChoicesDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxCurrentAnswerChoicesDropDownList.SelectedIndexChanged
        Me.uxAddAnswerChoiceTextBox.Text = Me.uxCurrentAnswerChoicesDropDownList.SelectedItem.Text.Trim()
    End Sub

    Protected Sub uxAddChoiceButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddChoiceButton.Click
        Dim QC As FormQuestionRowsDetail = FormQuestionRowsDetail.NewFormQuestionRowsDetail()

        QC.FormQuestionID = FormQuestionGuid
        QC.RowChoice = Me.uxAddAnswerChoiceTextBox.Text.Trim()

        QC.Save()

        BindSelectionListItems(FormQuestionGuid)
    End Sub

    Protected Sub uxDeleteChoiceButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeleteChoiceButton.Click
        FormQuestionRowsDetail.DeleteFormQuestionRowsDetail(ConvertToGuid(Me.uxCurrentAnswerChoicesDropDownList.SelectedValue.ToString().Trim()))

        BindSelectionListItems(FormQuestionGuid)
    End Sub

    Protected Sub uxUpdateChoiceButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxUpdateChoiceButton.Click
        Dim QC As FormQuestionRowsDetail = FormQuestionRowsDetail.GetFormQuestionRowsDetail(ConvertToGuid(Me.uxCurrentAnswerChoicesDropDownList.SelectedValue.ToString().Trim()))

        QC.RowChoice = Me.uxAddAnswerChoiceTextBox.Text.Trim()

        QC.Save()

        BindSelectionListItems(FormQuestionGuid)
    End Sub

    Protected Sub uxAddColumnButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddColumnButton.Click
        Dim QC As FormQuestionColumsDetail = FormQuestionColumsDetail.NewFormQuestionColumsDetail()

        QC.FormQuestionID = FormQuestionGuid
        QC.ColumnChoice = Me.uxAddAnswerColumnTextBox.Text.Trim()

        QC.Save()

        BindSelectionListItems(FormQuestionGuid)
    End Sub

    Protected Sub uxDeleteColumnButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeleteColumnButton.Click
        FormQuestionColumsDetail.DeleteFormQuestionColumsDetail(ConvertToGuid(Me.uxCurrentAnswerColumnsDropDownList.SelectedValue.ToString().Trim()))

        BindSelectionListItems(FormQuestionGuid)
    End Sub

    Protected Sub uxUpdateColumnButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxUpdateColumnButton.Click
        Dim QC As FormQuestionColumsDetail = FormQuestionColumsDetail.GetFormQuestionColumsDetail(ConvertToGuid(Me.uxCurrentAnswerColumnsDropDownList.SelectedValue.ToString().Trim()))

        QC.ColumnChoice = Me.uxAddAnswerColumnTextBox.Text.Trim()

        QC.Save()

        BindSelectionListItems(FormQuestionGuid)
    End Sub

    Protected Overloads Sub uxEditButton_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        FormQuestionGuid = ConvertToGuid(e.CommandArgument.ToString())
        LoadEditDetails(FormQuestionGuid)
    End Sub

    Protected Overloads Sub uxAddQuestion_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        FormQuestionGuid = Guid.Empty
        ClearEditDetails()
    End Sub

    Protected Overloads Sub uxDeleteButton_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        FormQuestionDetail.DeleteFormQuestionDetail(ConvertToGuid(e.CommandArgument.ToString()))

        BuildPage(FormGuid, Guid.Empty, Me.uxControlsPlaceHolder, "formbuilder", False)
    End Sub

#End Region

#Region " Helpers and Methods "

	Protected Sub GetQueryStringValues()
		If FormGuid = Guid.Empty Then
			FormGuid = GetGuidFromQueryString("FormID")
		End If
	End Sub

	Private Sub SetAnswerChoicePanelState(ByVal value As Boolean, ByVal EditMode As Boolean)

		If EditMode Then

			Me.uxEditChoicesPanel.Visible = value
			Me.uxAddAnswerChoiceRequiredfieldvalidator.Enabled = value
			Me.uxAnswerChoicesPanel.Visible = value

			SetBuildChoicesPanelState(False)

		Else
			Me.uxEditChoicesPanel.Visible = False

			Me.uxAddAnswerChoiceRequiredfieldvalidator.Enabled = False
			Me.uxAnswerChoicesPanel.Visible = value
			Me.uxAnswerChoicesRequiredfieldvalidator.Enabled = value
		End If

	End Sub

	Private Sub SetAnswerColumnPanelState(ByVal value As Boolean, ByVal EditMode As Boolean)

		If EditMode Then

			Me.uxEditColumnsPanel.Visible = value
			Me.uxAddAnswerColumnRequiredfieldvalidator.Enabled = value
			Me.uxAnswerColumnPanel.Visible = value

			SetBuildColumnPanelState(False)

		Else

			Me.uxEditColumnsPanel.Visible = False

			Me.uxEditColumnsPanel.Visible = False
			Me.uxAnswerColumnPanel.Visible = value
			Me.uxAddAnswerColumnRequiredfieldvalidator.Enabled = False

		End If

	End Sub

    Private Sub SetBuildChoicesPanelState(ByVal value As Boolean)
        Me.uxBuildChoicesPanel.Visible = value
    End Sub

    Private Sub SetBuildColumnPanelState(ByVal value As Boolean)
        Me.uxBuildColumnPanel.Visible = value
    End Sub

    Private Sub SetRequiredPanelState(ByVal value As Boolean)
        Me.uxRequiredPanel.Visible = value
    End Sub

    Private Sub SetValidatorPanelState(ByVal value As Boolean)
        Me.uxValidatorPanel.Visible = value
    End Sub

    Private Sub SetComparePanelState(ByVal value As Boolean)
    End Sub

    Private Sub SetMultiTextBoxPanelState(ByVal value As Boolean)
        Me.uxMultiTextBoxPanel.Visible = value
    End Sub

    Private Sub SetChoiceLayoutPanelState(ByVal value As Boolean)
        Me.uxChoiceLayoutPanel.Visible = value
    End Sub

    Private Sub SetQuestionAttributesPanelState(ByVal value As Boolean)
        Me.uxQuestionAttributesPanel.Visible = value
	End Sub

	Private Sub SetuxOtherOptionPanelState(ByVal value As Boolean)
		Me.uxOtherOptionPanel.Visible = value
	End Sub

#End Region

#Region " Form/Survey Page Builders "

    Protected Overloads Sub BuildPage(ByVal FormGuid As Guid, ByVal FormResponseGuid As Guid, ByVal ControlsPlaceholder As PlaceHolder, ByVal ValidationGroup As String, ByVal LoadAnswers As Boolean)
        Dim list As New FormList
        Dim DT As New DataTable
        Dim DR As DataRow

        DT = list.GetControls(FormGuid)

        ControlsPlaceholder.Controls.Clear()

        Dim table As New HtmlTable
        table.ID = "uxControlsTable"
        table.Width = "100%"

        If DT.Rows.Count > 0 Then
			For Each DR In DT.Rows

				Dim row As New HtmlTableRow
				Dim questionrow As New HtmlTableRow
				Dim questioncell As New HtmlTableCell
				Dim lbl As New Label

				row.ID = String.Format("ux_{0}_TableRow", DR("formquestionid").ToString())
				questionrow.ID = String.Format("ux_{0}_TableHeaderRow", DR("formquestionid").ToString())
				questioncell.ID = String.Format("ux_{0}_QuestionCell", DR("formquestionid").ToString())

				lbl.ID = String.Format("ux_{0}_Label", DR("formquestionid").ToString())
				lbl.Text = DR("question").ToString()

				questioncell.ColSpan = 2
				questioncell.Attributes.Add("class", "TDNAME")
				questioncell.Controls.Add(lbl)

				questionrow.Cells.Add(questioncell)
				row.Cells.Add(BuildEditCell(DR("formquestionid").ToString()))
				row.Cells.Add(BuildControlCell(DR("formquestionid").ToString(), FormResponseGuid, CType(DR("controltype"), FormControlType), ValidationGroup, LoadAnswers, False))

				table.Rows.Add(questionrow)
				table.Rows.Add(row)

			Next
        End If

        ControlsPlaceholder.Controls.Add(table)

    End Sub

    Private Function BuildEditCell(ByVal FormQuestionID As String) As HtmlTableCell

        Dim cell As New HtmlTableCell

        cell.Controls.Add(BuildEditButton(FormQuestionID))
        cell.Controls.Add(BuildDeleteButton(FormQuestionID))

        cell.ID = String.Format("ux_{0}_EditCell", FormQuestionID)
        cell.Attributes.Add("class", "TDVALUE")

        Return cell

	End Function


#Region " Build General Controls "

    Private Function BuildEditButton(ByVal ID As String) As Button

        Dim btn As New Button

        btn.Text = Resources.Resource.Edit
        btn.ID = String.Format("{0}_EditButton", ID)
        btn.CommandArgument = ID
        btn.CommandName = "uxEditButton_Command"
        btn.CssClass = "SMBUTTON"
        btn.Attributes.Add("onmouseover", "this.className='SMBUTTONOVER'")
        btn.Attributes.Add("onmouseout", "this.className='SMBUTTONOUT'")
        AddHandler btn.Command, AddressOf uxEditButton_Command

        Return btn

    End Function

    Private Function BuildDeleteButton(ByVal ID As String) As Button

        Dim btn As New Button

        btn.Text = Resources.Resource.Delete
        btn.ID = String.Format("{0}_DeleteButton", ID)
        btn.CommandArgument = ID
        btn.CommandName = "uxDeleteButton_Command"
        btn.CssClass = "SMBUTTON"
        btn.Attributes.Add("onmouseover", "this.className='SMBUTTONOVER'")
        btn.Attributes.Add("onmouseout", "this.className='SMBUTTONOUT'")
        AddHandler btn.Command, AddressOf uxDeleteButton_Command

        Return btn

    End Function

#End Region

#End Region

End Class
